<?php

namespace App\Http\Controllers\Api\V1\Frontend\Test;

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;

class TestController extends BaseController
{
    /**
     * wsdl 地址
     */
    const URL = 'http://9.23.16.139:30008/wechatBase-service/services/ExternalMessageService?wsdl';

    /**
     * 生产提示语
     * @return string
     */
    public function getContent() : string
    {
        return "服务单名称：消息推送测试服务单名<br/> 编号：TEST0000 <br/>逾期：7天，请及时审批";
    }

    /**
     * 采用测试数据进行发送消息
     * @param Request $request
     */
    public function send(Request $request)
    {
        if (!$request->filled('system_id')) {
            $request->offsetSet('system_id','OKIT');
        }

        if (!$request->filled('system_Identifier')) {
            $request->offsetSet('system_Identifier','OKIT0000');
        }

        if (!$request->filled('system_password')) {
            $request->offsetSet('system_password','OKIT9999');
        }

        if (!$request->filled('content')) {
            $request->offsetSet('content',$this->getContent());
        }

        if (!$request->filled('user_number')) {
            $request->offsetSet('user_number','110104198308083010');
        }

        try {
            $service = new \SoapClient(self::URL,[
                'trace' => true
            ]);

            $parameters = [
                'in' => [
                    'sys_id'      => $request->input('system_id'),
                    'sys_id_pass' => $request->input('system_password'),
                    'sys_flag'    => $request->input('system_Identifier'),
                    'userType'    => '2',
                    'userNum'     => $request->input('user_number'),
                    'content'     => $request->input('content')
                ]
            ];

            $result = $service->sendTextMsg($parameters);

            dd($result);
        } catch (\Exception $exception) {
            dd('推送失败，失败原因：'.$exception->getMessage());
        }
    }
}
